package com.heyue.ems.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.heyue.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.heyue.common.log.annotation.Log;
import com.heyue.common.log.enums.BusinessType;
import com.heyue.common.security.annotation.PreAuthorize;
import com.heyue.ems.domain.EmsOrder;
import com.heyue.ems.service.IEmsOrderService;
import com.heyue.common.core.web.controller.BaseController;
import com.heyue.common.core.web.domain.AjaxResult;
import com.heyue.common.core.utils.poi.ExcelUtil;

/**
 * 同行单（业务）Controller
 * 
 * @author WChu
 * @date 2021-07-26
 */
@RestController
@RequestMapping("/emsOrder")
public class EmsOrderController extends BaseController
{
    @Autowired
    private IEmsOrderService emsOrderService;


    /**
     * 查询同行单（业务）列表
     * @return
     */
    @PreAuthorize(hasPermi = "ems:emsOrder:list")
    @GetMapping("/list")
    public Object[] list(EmsOrder emsOrder)
    {
//        startPage();
//        List<EmsOrder> list = emsOrderService.selectEmsOrderList(emsOrder);
//        return getDataTable(list);

        Object[] ob  = new Object[3];
        Boolean flag = emsOrderService.isAdmin();
        ob[0] = flag;
        startPage();
        List<EmsOrder> list = emsOrderService.selectEmsOrderList(emsOrder);
        ob[1] = getDataTable(list);
        String userName = SecurityUtils.getUsername();
        ob[2] = userName;
        return ob;
    }

    @GetMapping("/isAdmin")
    public Boolean isAdmin(){
        Boolean flag = emsOrderService.isAdmin();
        return flag;
    }


    /**
     * 导出同行单（业务）列表
     */
    @PreAuthorize(hasPermi = "ems:emsOrder:export")
    @Log(title = "同行单（业务）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsOrder emsOrder) throws IOException
    {
        List<EmsOrder> list = emsOrderService.selectEmsOrderList(emsOrder);
        ExcelUtil<EmsOrder> util = new ExcelUtil<EmsOrder>(EmsOrder.class);
        util.exportExcel(response, list, "同行单（业务）数据");
    }

    /**
     * 获取同行单（业务）详细信息
     */
    @PreAuthorize(hasPermi = "ems:emsOrder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(emsOrderService.selectEmsOrderById(id));
    }

    /**
     * 新增同行单（业务）
     */
    @PreAuthorize(hasPermi = "ems:emsOrder:add")
    @Log(title = "同行单（业务）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsOrder emsOrder)
    {
        return toAjax(emsOrderService.insertEmsOrder(emsOrder));
    }

    /**
     * 修改同行单（业务）
     */
    @PreAuthorize(hasPermi = "ems:emsOrder:edit")
    @Log(title = "同行单（业务）", businessType = BusinessType.UPDATE)
    @PutMapping
    public Object[] edit(@RequestBody EmsOrder emsOrder)
    {
        Boolean flag = emsOrderService.isAdmin();
//        System.out.println(flag);
        Object[] ob  = new Object[4];
        ob[0] = flag;
        ob[1] = toAjax(emsOrderService.updateEmsOrder(emsOrder));
        String userName = SecurityUtils.getUsername();
        ob[2] = userName;
        long userId = SecurityUtils.getUserId();
        ob[3] = userId;
//        System.out.println(ob[0]);
//        System.out.println(ob[1]);
        return ob;
//        return toAjax(emsOrderService.updateEmsOrder(emsOrder));

    }

    /**
     * 删除同行单（业务）
     */
    @PreAuthorize(hasPermi = "ems:emsOrder:remove")
    @Log(title = "同行单（业务）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emsOrderService.deleteEmsOrderByIds(ids));
    }
}
